package com.muyi.controller.comment;

import com.alibaba.fastjson.JSON;
import com.muyi.pojo.vo.Result;
import com.muyi.service.CommentService;
import com.muyi.service.impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletDeleteCommentById", value = "/deleteCommentById")
public class ServletDeleteCommentById extends HttpServlet {
    private CommentService commentService = new CommentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取请求参数
        int comment_id = Integer.parseInt(request.getParameter("comment_id"));
        //调用service层方法
        Result result = null;
        try {
            result = commentService.deleteCommentById(comment_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将数据返回给前端
        response.getWriter().write(JSON.toJSONString(result));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
